package com.baseDemo.web.service.user.impl;



import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.baseDemo.web.dao.user.UserEntityDao;
import com.baseDemo.web.entity.AuthorityEntity;
import com.baseDemo.web.entity.RoleEntity;
import com.baseDemo.web.entity.UserEntity;
import com.baseDemo.web.server.SystemServer;
import com.baseDemo.web.service.user.UserService;
import com.share.MD5Util;
import com.share.hibernate.baseDaoSupport.PageUtil;
import com.share.service.baseServiceSupport.impl.BaseServiceSupport;
@Service
public class UserServiceImpl extends BaseServiceSupport<UserEntity> implements UserService{
	@Resource
	private UserEntityDao userEntityDao;
	@Resource PageUtil pageUtil;

	@Override
	public String doLogin(String userName, String password,
			HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		String newpass=null;
		if(!password.equals("")){
			newpass=MD5Util.getMD5String(password);
		}
		UserEntity userEntity = userEntityDao.getByUsername(userName, newpass);
		if(userEntity!=null){
			if(userEntity.isActive()){
				Set<RoleEntity> roles = userEntity.getRoles();
				Set<AuthorityEntity> userAuth = new HashSet<AuthorityEntity>();
				if(roles!=null&&roles.size()>0){
					for (RoleEntity role : roles) {
						Set<AuthorityEntity> authorities = role.getAuthorities();
						if(authorities!=null&&authorities.size()>0){
							userAuth.addAll(authorities);
						}
					}
				}
				userEntity.setAuthorities(userAuth);
				session.setAttribute(SystemServer.getInstance().get("USERSESSION").toString(), userEntity);//保存用户信息到session中
				return String.valueOf(1);//登录成功
			}else{
				return String.valueOf(2);//用户锁定
			}
		}else{
			return String.valueOf(0);//用户名或密码错误
		}
	}

	@Override
	public String loginOut(HttpSession session) {
		Object object=session.getAttribute(SystemServer.getInstance().get("USERSESSION").toString());
		if (object!=null) {
			session.removeAttribute(SystemServer.getInstance().get("USERSESSION").toString());
			return String.valueOf(1);
		}
		return String.valueOf(0);
	}

	@Override
	public PageUtil getListForPageToJson(Integer start, Integer limit) {
		Integer rowTotal = userEntityDao.getPageTotal();
		pageUtil.setTotal(rowTotal);
		List<UserEntity> list = userEntityDao.find(limit, start);
		pageUtil.setEntityList(list);
		return pageUtil;
	}

	@Override
	public String addUser(UserEntity userEntity) {
		String pass = userEntity.getPassword();
		if(pass==null||"".equals(pass)){
			pass=SystemServer.getInstance().get("DEFAULTPASS").toString();
		}
		userEntity.setPassword(MD5Util.getMD5String(pass));
		return super.save(userEntity);
	}

	@Override
	public UserEntity getByLoginId(String loginId) {
		return userEntityDao.getByUsername(loginId);
	}

	@Override
	public String resetPassword(UserEntity userEntity) {
		userEntity.setPassword(MD5Util.getMD5String(SystemServer.getInstance().get("DEFAULTPASS").toString()));
		return super.update(userEntity);
	}

	@Override
	public String changePassword(HttpServletRequest request, String password) {
		HttpSession session = request.getSession();
		UserEntity userEntity=(UserEntity)session.getAttribute(SystemServer.getInstance().get("USERSESSION").toString());
		if(userEntity!=null){
			if(!password.equals("")){
				return "{'success':'"+userEntityDao.updatePassword(MD5Util.getMD5String(password), userEntity.getId())+"'}";
			}
		}
		return "{'success':'0'}";
	}
}
